<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\controller\driver;
use think\Controller;
/**
 * @name【加油管理】
 * @baseurl【driver.index】
 */
class Oil extends Base
{
    /**
     * @name【加油记录】
     * @desc【加油记录】
     * @uri【addOil】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * add_count $ float $ 5.1 $ 升数 $1#
     * add_money $ float $ 60 $ 金额 $1#
     * car_meter $ int $ 3456 $ 里程数 $1#
     * oil_pic $ string $ 图片 $ 加油小票 $1#
     * addr $ string $ 福建 $ 地址 $1 #
     * oil_level $ string $ 92 $油标 $ 1#
     * station $ string $ 中石化 $ 加油站 $ 1#
     * car_pic $ string $ 图片 $ 里程照片 $1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * 】
     */
    public function addOil(){
        $this->auth();
        $data['driver_id']      = $this->driverInfo['driver_id'];
        $driver                 = \app\api\model\Driver::get($this->driverInfo['driver_id']);
        $data['car_id']         = $driver->car ? $driver->car->car_id :0;
        if($data['car_id']==0){
            return $this->retError(505);
        }

        $data['phone']          = $this->driverInfo['phone'];
        $data['fuel_man']       = $this->driverInfo['name'];
        $data['driver_name']    = $this->driverInfo['name'];
        $data['car_number']     = $driver->car->number;
        $data['start_date']     = date('Y-m-d H:i:s');
        $data['gas_station']    = input('station');
        $data['refuel_add']     = input('addr');
        $data['label']          = input('oil_level');
        $data['litre']          = input('add_count',0);
        $data['price']          = input('add_money',0);
        $data['per']            = $data['price']==0 ? 0 : $data['price']/$data['litre'];
        $data['km']             = input('car_meter',0);
        $carOil                 = new \app\api\model\RefuelRecord();
        $preAddOil              = $carOil->where("car_id =".$data['car_id'])->order("refuel_record_id desc")->find();
        $data['km_add']         = $preAddOil['km'] ? $data['km'] - $preAddOil['km'] : 0;
        $ret                    = $this->saveBase64(input('oil_pic',''));
        if($ret[0]==0){
            return $this->retError($ret[1]);
        }
        $data['img']            = $ret[1];
        $ret                    = $this->saveBase64(input('car_pic'));
        if($ret[0]==0){
            return $this->retError($ret[1]);
        }
        $data['km_img']         = $ret[1];
        if($carOil->validate(true)->save($data)){
            return $this->retSucc();
        }else{
            return $this->retError($carOil->getError());
        }
    }
    
    /**
     * @name【删除加油记录】
     * @desc【删除加油记录】
     * @uri【delOil】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * car_oil_id $ int $ 1 $ 加油记录id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * 】
     */
    public function delOil(){
        $this->auth();
        $where['driver_id']      = $this->driverInfo['driver_id'];
        $where['car_oil_id']     = input('car_oil_id',0);
        if(\app\api\model\CarOil::destroy($where)){
            return $this->retSucc();
        }else{
            return $this->retError(113);
        }
    }
    
    /**
     * @name【查看加油记录】
     * @desc【删除加油记录】
     * @uri【delOil】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * car_oil_id $ int $ 1 $ 加油记录id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息 $ {"car_oil_id":1713,"add_date":"2018-01-26","add_count":"0","add_money":"0.00","price":"0.00","station":"石化","oil_pic":"upload\/driver\/img\/20180126\/1516965717.jpeg","car_pic":"upload\/driver\/img\/20180126\/1516965717.jpeg","car_meter":11,"delete_time":null,"driver_name":"帅哥","oil_level":"0","addr":"测试"}
     * 】
     */
    public function viewOil(){
        $this->auth();
        $where['driver_id']             = $this->driverInfo['driver_id'];
        $where['refuel_record_id']      = input('car_oil_id',2);
        $field                          = 'refuel_record_id car_oil_id,start_date add_date,litre add_count,price add_money,per price,gas_station station';
        $field                         .= ',img oil_pic,km_img car_pic,km car_meter,delete_time,fuel_man driver_name,label oil_level,refuel_add';
        $data['data']                   = (new \app\api\model\RefuelRecord())->where($where)->field($field)->find();
        if(empty($data['data'])){
            return $this->retError(113);
        }else{
            return $this->retSucc($data);
        }
    }

    /**
     * @name【加油列表】
     * @desc【加油列表】
     * @uri【listOil】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1#
     * page   $ int $ 1 $ 页数 #
     * pagenum $ num $ 10 $ 记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $string $ $ 列表 $ [{"car_oil_id":1712,"add_date":"2018-01-26","add_count":"0","add_money":"0.00","price":null,"station":null,"oil_pic":"upload\/driver\/img\/20180126\/1516958859.jpeg","car_pic":"upload\/driver\/img\/20180126\/1516958859.jpeg","car_meter":11,"delete_time":null,"driver_name":"帅哥","oil_level":"0","addr":null},{"car_oil_id":1713,"add_date":"2018-01-26","add_count":"0","add_money":"0.00","price":"0.00","station":"石化","oil_pic":"upload\/driver\/img\/20180126\/1516965717.jpeg","car_pic":"upload\/driver\/img\/20180126\/1516965717.jpeg","car_meter":11,"delete_time":null,"driver_name":"帅哥","oil_level":"0","addr":"测试"}]
     * 】
     */
    public function listOil(){
        $this->auth();
        $where['driver_id']             = $this->driverInfo['driver_id'];
        $field                          = 'refuel_record_id car_oil_id,start_date add_date,litre add_count,price add_money,per price,gas_station station';
        $field                         .= ',img oil_pic,km_img car_pic,km car_meter,delete_time,fuel_man driver_name,label oil_level,refuel_add addr';
        $data['data']                   = (new \app\api\model\RefuelRecord())->where($where)->field($field)->page(input('page',1),input('pagenum',10))->select();
        if(empty($data['data'])){
            return $this->retError(113);
        }else{
            return $this->retSucc($data);
        }
    }
    
    /**
     * 图片上传 后台调用不开放接口
     */
    public function upload($file){
        if ((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg") || ($file["type"] == "image/pjpeg")) && ($file["size"] < 20000)){
            if ($file["error"] > 0){
                return [0,$file["error"]];
            }else{
                $imgName = date('YmdHis');
                move_uploaded_file($file["tmp_name"],"upload/" . $file["name"]);
                return [1,'ok'];
            }
        }else{
            return [0,"Invalid file"];
        }
    }
}